<?php
namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\NewRoomServices;
use App\Http\Services\RoomServicesInit;
use App\Http\Services\RoomMsgServices;
use App\Http\Services\MsgServices;
use App\Http\Services\MusicServices;
use App\Http\Model\RoomMicro;

class ApiRoomController extends BaseController
{
    protected $roomServices;

    protected $anchorStartLiveTime = 'MICLIVE:START:USER:'; //主播上麦缓存 MICLIVE:START:USER: userid=>time string
    protected $anchorLiveTime = 'MICLIVETIME:USER:';//主播开播总时间秒 MICLIVETIME:USER: userid=>int 

    public function __construct()
    {
        $this->roomServices = new NewRoomServices();

    }

    /**
     * 更新开厅时长 脚本
     */
    public function setOnlineTime(){
        return $this->responseData($this->roomServices->setOnlineTime());
    }

    /**
     * 更新房间热度和开启时长 脚本
     */
    public function setRoomhot(){
        return $this->responseData($this->roomServices->setRoomhot());
    }


    /**
     * 记录房间用户聊天
     */
    public function saveUserChat(Request $request)
    {
        $roomId = $request->input("room_id");
        $content = $request->input("content");
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $this->roomServices->saveUserChat($roomId, $userId, $content);

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 获取已开启的房间列表
     */
    public function getOpenRoomList(Request $request)
    {
        $userId = $this->getLoginUserId();
        $type = (int) $request->input('type', -1);
        $page = (int) $request->input('page', 0);

        $roomList = $this->roomServices->getOpenRoomList($userId, $type, $page);

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 普通麦下麦
     */
    public function roomMicroOut(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->roomServices->roomMicroOut($roomId, $userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 普通麦抱下麦
     */
    public function roomMicroBeOut(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        $micro_id = $request->input("microphone_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if($micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        if (!redis_lock('ROOM:MICRO:BEOUT:'.$roomId.':'.$micro_id, 1)){
            Response::returnResult(Hint::NOT_ALLOWED);
        }

        $data = $this->roomServices->roomMicroBeOut($roomId, $micro_id, $userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 主持麦抱下麦
     */
    public function roomHostMicroBeOut(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->roomServices->roomHostMicroBeOut($roomId, $userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 房间解除用户踢出
     */
    public function revokeShotoff(Request $request)
    {
        $roomId = $request->input("room_id");
        $toUserId = $request->input("user_id");
        if (empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (empty($toUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $roomUserId = $this->getLoginUserId();
        if(empty($roomUserId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->roomServices->revokeShotoff($roomId, $roomUserId, $toUserId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 离开房间
     */
    public function roomUserQuitV1(Request $request)
    {
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 离开房间
     */
    public function roomUserQuit(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = $request->input('room_id');
        if (empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->roomServices->roomUserQuit($roomId, $userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 添加房间欢迎语
     */
    public function addRoomWelcomeWord(Request $request)
    {
        $loginUserId = $this->getLoginUserId();
        if(empty($loginUserId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = $request->input("room_id");
        $content = $request->input("content");
        if(empty($roomId) || empty($content)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->roomServices->addRoomWelcomeWord($loginUserId, $roomId, $content);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 编辑房间欢迎语
     */
    public function editRoomWelcomeWord(Request $request)
    {
        $loginUserId = $this->getLoginUserId();
        if(empty($loginUserId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $id = $request->input("id");
        $content = $request->input("content");
        if(empty($id) || empty($content)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->roomServices->editRoomWelcomeWord($loginUserId, $id, $content);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 删除房间欢迎语
     */
    public function delRoomWelcomeWord(Request $request)
    {
        $loginUserId = $this->getLoginUserId();
        if(empty($loginUserId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $ids = (string) $request->input("id");
        $ids = explode(',', $ids);
        if(empty($ids)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->roomServices->delRoomWelcomeWord($loginUserId, $ids);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 房间欢迎语列表
     */
    public function listRoomWelcomeWord(Request $request)
    {
        $loginUserId = $this->getLoginUserId();
        if(empty($loginUserId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->roomServices->listRoomWelcomeWord($loginUserId, $roomId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 获取房间欢迎语随机一条
     */
    public function randRoomWelcomeWord(Request $request)
    {
        $loginUserId = $this->getLoginUserId();
        if(empty($loginUserId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->roomServices->randRoomWelcomeWord($loginUserId, $roomId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 获取自己所拥有的CBD房间
     */
    public function selfRoomList(Request $request){
        $loginUserId = $this->getLoginUserId();
        if(empty($loginUserId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->roomServices->selfRoomList($loginUserId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 每小时房间结算流水
     */
    public function hourRoomBill(Request $request){
        $loginUserId = $this->getLoginUserId();
        if(empty($loginUserId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $nowRoomId = $request->input('room_id');
        if(empty($nowRoomId)){
            Response::returnResult(Hint::PARAM_ERROR,[],'不在房间');
        }
        $date = $request->input('date');
        $data = $this->roomServices->hourRoomBill($loginUserId, $nowRoomId, $date);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 房间锁麦
     */
    public function roomLockMicro(Request $request)
    {
        $roomId = (int) $request->input("room_id");
        $microId = (int) $request->input("micro_id", 0);
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $lockKey = 'ROOMMICRO:LOCK:'.$roomId;
        if (!redis_lock($lockKey, 10)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->roomServices->roomLockMicro($roomId, $userId, $microId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 房间解除锁麦
     */
    public function roomUnLockMicro(Request $request)
    {
        $roomId = (int) $request->input("room_id");
        $microId = (int) $request->input("micro_id", 0);
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $lockKey = 'ROOMMICRO:LOCK:'.$roomId;
        if (!redis_lock($lockKey, 10)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->roomServices->roomUnLockMicro($roomId, $userId, $microId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 下麦确认
     */
    public function outMicroConfirm(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->roomServices->outMicroConfirm($roomId, $userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 上麦确认
     */
    public function intoMicroConfirm(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->roomServices->intoMicroConfirm($roomId, $userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 拍拍房开始牵线
     */
    public function roomPaiPaiOpen(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $lockKey = 'ROOMPAIPAI:OPEN:'.$roomId;
        if (!redis_lock($lockKey, 10)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->roomServices->roomPaiPaiOpen($roomId, $userId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 拍拍房加时
     */
    public function roomPaiPaiAddTime(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $lockKey = 'ROOMPAIPAI:OPEN:'.$roomId;
        if (!redis_lock($lockKey, 10)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->roomServices->roomPaiPaiAddTime($roomId, $userId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 拍拍房查询结果
     */
    public function roomPaiPaiGetResult(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->roomServices->roomPaiPaiGetResult($roomId, $userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 拍拍房确定结果
     */
    public function roomPaiPaiDefine(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->roomServices->roomPaiPaiDefine($roomId, $userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 拍拍房结束并派发
     */
    public function roomPaiPaiEnd(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        $rankId = (int) $request->input("rank_id");
        $day = (int) $request->input("day");
        if(empty($roomId) || empty($rankId) || empty($day)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->roomServices->roomPaiPaiEnd($roomId, $userId, $rankId, $day);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 获取当前热度最高的拍拍房
     */
    public function roomPaiPaiHotMax(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $data = $this->roomServices->roomPaiPaiHotMax($userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * ceshi
     */
    public function handleRoomPaiPai(Request $request)
    {
        $data = $this->roomServices->handleRoomPaiPai(['room_id'=>10013]);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

}
